package com.jingfu.controller;

import com.jingfu.Service.DeptmentService;
import com.jingfu.dto.TreeDTO;
import com.jingfu.dto.UserDeptDTO;
import com.jingfu.util.CommonResult;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.Arrays;
import java.util.List;

/**
 * @author jingfuu@163.com
 * @version v1.0
 * @date 2022/2/8 下午2:46
 **/
@Controller
@Slf4j
public class DeptmentController {
    @Autowired
    DeptmentService deptmentService;

    @RequestMapping("/deptment-view")
    public String getDeptmentView() {
        return "system/deptment";
    }

    @RequestMapping("/deptment-add-view")
    public String getDeptmentAddView() {
        return "system/operate/deptAdd";
    }

    @RequestMapping("/deptment/tree")
    @ResponseBody
    public CommonResult<List<TreeDTO>> getDeptmentTree() {
        return CommonResult.ok(deptmentService.getDeptmentTree());
    }

    /**
     * 清除用户部门关系
     * @param userId
     * @return
     */
    @RequestMapping("/deptment/batch-remove/{userId}")
    @ResponseBody
    public CommonResult batchDeleteRole(@PathVariable String userId) {
        log.info("请求批量清除用户部门关系，用户Id:{}", userId);
        Arrays.stream(userId.split(",")).forEach(id -> {
            deptmentService.deleteUserDept(id);
        });
        return CommonResult.ok().build();
    }

    /**
     * 批量绑定用户与部门关系
     * @param userDeptDTO
     * @return
     */
    @RequestMapping("/batch-create/user/dept")
    @ResponseBody
    public CommonResult batchCreateUserDept(@RequestBody UserDeptDTO userDeptDTO) {
        deptmentService.batchCreateUserDept(userDeptDTO);
        return CommonResult.ok().build();
    }
}
